




C Control Statements Test 1
C control statements test paper contains questions from decision statement: if-else and switch, loop statement: for loop, while loop & do-while loop and jump statement: break and continue.

1) Which data type cannot be checked in switch-case statement?

enum
character
integer
float

Show Answer

The correct option is (d).
Explanation:
In C-languageswitch/case statement is defined by the language specification to use an int value therefore we cannot use a float value in switch/case statement.

2) How many times "javaTpoint" is printed?

#include
int main()
{
    int x;
    for(x=-1; x<=10; x++)
    {
        if(x < 5)
            continue;
        else
            break;
        printf("javaTpoint");
    }
    return 0;
}


10 times
11 times
0 times
Infinite times

Show Answer

The correct option is (c).
Explanation:
In program the x is initialized with -1. As x < 5 (since x is -1) it will start with continue statement.
Continue means "stop the current iteration and proceed to the next iteration". Therefore  x becomes 0 now. This will take place until x becomes 5.
Now if the value of x=5, it will enter the else part where it encounters the break statement, as a result it will come out of for loop. Hence it will not go to printf statement.
Therefore javaTpoint will be printed 0 times.

3) How many times while loop is executed if a short int is 2 byte wide?

#include
int main()
{
    int i=1;
    while(i <= 155)
    {
        printf("%c %d\n", i, i);
        i++;
    }
    return 0;
}


154 times
155 times
156 times
Infinite times

Show Answer

The correct option is (b).
Explanation:
The size of short int which is 2 byte wide does not affect the while() loop operation.
Therefore thewhile (i <= 155) loop will executed 155 times.

4) Which statement is correct about the below program?

#include
int main()
{
    int i = 8, j = 24;
    if(i = 8) && if(j = 24)
        printf("Welcome Programmer");
    return 0;
}



Welcome Programmer
Error: Undeclared identifier if
Error: Expression syntax
No output

Show Answer

The correct option is (c).
Explanation:
In program 5th line i.e. if(i = 8) && if(j = 24) the "Expression syntax" error occur.
Hence the statement should be like if((i == 5) && (j == 10)).
Therefore on compiling the program Error: Expression syntax is occurring.

5) Find out the error, if any in the below program?

#include
int main()
{
    int j = 1;
    switch(j)
    {
        printf("Hello programmer!");
        case 1:
            printf("Case1");
            break;
        case 2:
            printf("Case2");
            break;
    }
return 0;
}


No error in program and prints "Case1"
Error: Invalid printf statement after switch statement
Error: No default specified
None of the above

Show Answer

The correct option is (a).
Explanation:
In program switch statement is used for switch(j) it becomes switch(1) because i is initialized with 1.
Therefore the case 1: block is get executed. Hence it prints "Case1".
Printf ("Hello programmer!"); is ignored by the compiler.
Hence there is no error in program and prints "Case1".












Please Share





